package edu.cth.scrabbit.client.view.panels;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.LayoutStyle;

import org.jdesktop.application.Application;

import edu.cth.scrabbit.client.ctrl.ClientController;
import edu.cth.scrabbit.shared.GameLetter;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class UserLetterPanel extends javax.swing.JPanel {
	private GameLetter[] gameLetters = new GameLetter[7];
	private JButton[] buttons = new JButton[7];

	private ActionListener buttonListener1;
	private ActionListener buttonListener2;
	private ActionListener buttonListener3;
	private ActionListener buttonListener4;
	private ActionListener buttonListener5;
	private ActionListener buttonListener6;
	private ActionListener buttonListener7;

	private JButton jButton1;
	private JButton jButton7;
	private JButton jButton6;
	private JButton jButton5;
	private JButton jButton4;
	private JButton jButton3;
	private JButton jButton2;

	public UserLetterPanel() {
		super();
		initGUI();

	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent) this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(358, 48));
			{
				jButton1 = new JButton();
				jButton1.setName("jButton1");
				jButton1.setFont(new Font("Arial", Font.PLAIN, 9));
				jButton1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{
				jButton3 = new JButton();
				jButton3.setName("jButton3");

				jButton3.setFont(new Font("Arial", Font.PLAIN, 9));
				jButton3.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{
				jButton4 = new JButton();
				jButton4.setName("jButton4");

				jButton4.setFont(new Font("Arial", Font.PLAIN, 9));
				jButton4.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{
				jButton5 = new JButton();
				jButton5.setName("jButton5");

				jButton5.setFont(new Font("Arial", Font.PLAIN, 9));
				jButton5.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{
				jButton6 = new JButton();
				jButton6.setName("jButton6");

				jButton6.setFont(new Font("Arial", Font.PLAIN, 9));
				jButton6.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{
				jButton7 = new JButton();
				jButton7.setName("jButton7");

				jButton7.setFont(new Font("Arial", Font.PLAIN, 9));
				jButton7.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{
				jButton2 = new JButton();
				jButton2.setName("jButton2");

				jButton2.setFont(new Font("Arial", Font.PLAIN, 9));
				jButton2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addGap(6)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(jButton5, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jButton6, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jButton7, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jButton2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jButton1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jButton3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jButton4, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
				.addGap(7));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jButton3, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jButton4, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jButton5, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jButton6, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jButton7, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE));

			Application.getInstance().getContext().getResourceMap(getClass())
					.injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}

		gameLetters[0] = new GameLetter();
		gameLetters[1] = new GameLetter();
		gameLetters[2] = new GameLetter();
		gameLetters[3] = new GameLetter();
		gameLetters[4] = new GameLetter();
		gameLetters[5] = new GameLetter();
		gameLetters[6] = new GameLetter();

		buttonListener1 = new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				ClientController.getInstance().select(gameLetters[0]);
				jButton1.setText(gameLetters[0].toString());
			}
		};
		buttonListener2 = new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				ClientController.getInstance().select(gameLetters[1]);
				jButton2.setText(gameLetters[1].toString());
			}
		};

		buttonListener3 = new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				ClientController.getInstance().select(gameLetters[2]);
				jButton3.setText(gameLetters[2].toString());
			}
		};

		buttonListener4 = new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				ClientController.getInstance().select(gameLetters[3]);
				jButton4.setText(gameLetters[3].toString());
			}
		};

		buttonListener5 = new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				ClientController.getInstance().select(gameLetters[4]);
				jButton5.setText(gameLetters[4].toString());
			}
		};

		buttonListener6 = new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				ClientController.getInstance().select(gameLetters[5]);
				jButton6.setText(gameLetters[5].toString());
			}
		};
		buttonListener7 = new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				ClientController.getInstance().select(gameLetters[6]);
				jButton7.setText(gameLetters[6].toString());
			}
		};

		jButton1.addActionListener(buttonListener1);
		jButton2.addActionListener(buttonListener2);
		jButton3.addActionListener(buttonListener3);
		jButton4.addActionListener(buttonListener4);
		jButton5.addActionListener(buttonListener5);
		jButton6.addActionListener(buttonListener6);
		jButton7.addActionListener(buttonListener7);

		buttons[0] = jButton1;
		buttons[1] = jButton2;
		buttons[2] = jButton3;
		buttons[3] = jButton4;
		buttons[4] = jButton5;
		buttons[5] = jButton6;
		buttons[6] = jButton7;

		setEnableAllButtons(false);
	}

	public void setLetters(GameLetter[] ownLetters) {
		for (int i = 0; i < ownLetters.length; i++) {
			gameLetters[i] = ownLetters[i];
		}

		setButtonLetters();
	}

	private void setButtonLetters() {
		if (gameLetters[0] != null) {
			jButton1.setText(gameLetters[0].toString());
		}
		if (gameLetters[1] != null) {
			jButton2.setText(gameLetters[1].toString());
		}
		if (gameLetters[2] != null) {
			jButton3.setText(gameLetters[2].toString());
		}
		if (gameLetters[3] != null) {
			jButton4.setText(gameLetters[3].toString());
		}
		if (gameLetters[4] != null) {
			jButton5.setText(gameLetters[4].toString());
		}
		if (gameLetters[5] != null) {
			jButton6.setText(gameLetters[5].toString());
		}
		if (gameLetters[6] != null) {
			jButton7.setText(gameLetters[6].toString());
		}

	}

	public void disableButton(GameLetter letter) {
		for (int i = 0; i < gameLetters.length; i++) {
			if (gameLetters[i] == letter) {
				buttons[i].setEnabled(false);
				break;
			}
		}
	}

	public void setEnableAllButtons(boolean b) {
		for (int i = 0; i < buttons.length; i++) {
			buttons[i].setEnabled(b);
		}
	}
}
